package com.stars.home.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * on 2022/9/26
 */
public class DayUtils {

    /** * 用于显示时间 */
    public static final String TODAY = "今天";
    public static final String YESTERDAY = "昨天";
    public static final String TOMORROW = "明天";
    public static final String BEFORE_YESTERDAY = "前天";
    public static final String AFTER_TOMORROW = "后天";


    /** * 获取对应时间戳转换的今天、明天。。。。的日期 * @param time * @return */
    public static String getToday(String dayTime)  {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = null;
        try {
            date1 = simpleDateFormat.parse(dayTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long ts = date1.getTime();
        String time = String.valueOf(ts);
        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        Date date = null;
        try{

            date = new Date(Long.parseLong(time) * 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        cal.setTime(date);

        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            return showDateDetail(diffDay, dayTime);

        }

        String ss[]=dayTime.split(" ");
        String s1[]=ss[0].split("-");

        return  s1[1]+"-"+s1[2];
    }

    /** * 将日期差显示为今天、明天或者星期 * @param diffDay * @param time * @return */
    private static String showDateDetail(int diffDay, String time){
        switch(diffDay){
            case 0:
                return TODAY;
            case 1:
                return TOMORROW;
            case 2:
                return AFTER_TOMORROW;
            case -1:
                return YESTERDAY;
            case -2:
                return BEFORE_YESTERDAY;
            default:

                String ss[]=time.split(" ");
                String s1[]=ss[0].split("-");

                return  s1[1]+"-"+s1[2];
        }
    }

}
